package com.literature.search.file.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import com.literature.search.core.entity.R.AjaxJson;
import com.literature.search.core.entity.esindex.HistoryKeywords;
import com.literature.search.file.entity.dto.SearchAllDTO;
import com.literature.search.file.entity.dto.SearchSingleDTO;
import com.literature.search.file.entity.dto.TopNDTO;
import com.literature.search.file.entity.vo.RecommendVO;
import com.literature.search.file.service.IElasticService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.elasticsearch.client.indices.AnalyzeResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Api(tags = "搜索")
@RestController
@RequestMapping("/search")
@SaCheckLogin
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ElasticController {

    private final IElasticService elasticService;

    @PostMapping("/searchById")
    @ApiOperation(value = "精确根据文件id查询文档", notes = "返回该文档所有的信息，包括每一页页面的内容")
    public AjaxJson searchById(@RequestParam String id) {
        return elasticService.searchById(id);
    }

    @PostMapping("/searchAllByKeyword")
    @ApiOperation(value = "根据关键字查询所有文档的内容", notes = "这个接口用于首页搜索，非每个文档的详情页内容。他会返回搜索的一个文档列表，每个文档包含一个命中页面")
    public AjaxJson searchAll(@RequestBody SearchAllDTO searchAllDTO) throws IOException {

        return elasticService.searchAllByKeyword(searchAllDTO);
    }

    @PostMapping("/searchSingle")
    @ApiOperation(value = "根据关键字查询单个文档的内容")
    public AjaxJson searchSingle(@RequestBody SearchSingleDTO searchSingleDTO) throws IOException {
        return elasticService.searchSingle(searchSingleDTO);
    }

    @PostMapping("/deleteById")
    @SaCheckRole(value = "admin")
    @ApiOperation(value = "根据id删除文档",hidden = true,notes = "需要管理员账号",tags = "admin")
    public AjaxJson deleteById(@RequestParam String id) {
        Boolean aBoolean = elasticService.deleteById(id);
        return AjaxJson.getSuccessData(aBoolean);
    }

    @ApiOperation(value = "查询Es中总共含有多少个文档",notes = "需要管理员账号",tags = "admin")
    @SaCheckRole(value = "admin")
    @GetMapping("/count")
    public AjaxJson count() {
        int count = elasticService.count();
        return AjaxJson.getSuccessData(count);
    }

    @ApiOperation(value = "猜你喜欢",notes = "根据用户最近浏览的文档，推荐相似的文档")
    @PostMapping("/recommendByRecentlyViewed")
    public AjaxJson recommendByRecentlyViewed() {
        List<RecommendVO> recommends = elasticService.recommendByRecentlyViewed();
        return AjaxJson.getSuccessData(recommends);
    }

    @ApiOperation(value = "排行",notes = "返回xx最多的前N个文档,xx可以是点赞数，下载数，收藏数，浏览数")
    @PostMapping("/topN")
    public AjaxJson topN(@RequestBody @Validated TopNDTO topNDTO) {
        List<RecommendVO> recommends = elasticService.topN(topNDTO);
        return AjaxJson.getSuccessData(recommends);
    }

    @ApiOperation(value = "热门文档推荐")
    @PostMapping("/recommendHotDocument")
    public AjaxJson recommendHotDocument(){
        return elasticService.recommendHotDocument();
    }


    @ApiOperation(value = "测试es的_analyze接口",hidden = true)
    @SaIgnore
    @GetMapping("/analyze")
    public AjaxJson analyze(@RequestParam String text) {
        List<AnalyzeResponse.AnalyzeToken> analyze = elasticService.analyze(text);
        return AjaxJson.getSuccessData(analyze);
    }

    @ApiOperation(value = "获取当前用户存储在es中的历史搜索关键字",hidden = true)
    @PostMapping("/getHistoryKeywords")
    public AjaxJson getHistoryKeywords() {
        List<HistoryKeywords> historyKeywords = elasticService.getHistoryKeywords();
        return AjaxJson.getSuccessData(historyKeywords);
    }



}